﻿using System;
using System.Windows.Input;

namespace DeepEarth.Core.Utilities
{
    public class LambdaCommand : ICommand
    {
        Action action;

        public LambdaCommand(Action action)
        {
            if (action == null) throw new ArgumentNullException("action");

            this.action = action;
        }

        #region Properties
        bool isEnabled = true;
        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                if (value != isEnabled)
                {
                    isEnabled = value;
                    if (CanExecuteChanged != null)
                        CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
        #endregion

        #region ICommand Members
        public bool CanExecute(object parameter)
        {
            return isEnabled;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            action();
        }
        #endregion
    }

    public class LambdaCommand<T> : ICommand
    {
        Action<T> action;

        public LambdaCommand(Action<T> action)
        {
            if (action == null) throw new ArgumentNullException("action");

            this.action = action;
        }

        #region Properties
        bool isEnabled = true;
        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                if (value != isEnabled)
                {
                    isEnabled = value;
                    if (CanExecuteChanged != null)
                        CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
        #endregion

        #region ICommand Members
        public bool CanExecute(object parameter)
        {
            return isEnabled;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            action((T)parameter);
        }
        #endregion
    }
}